//
// Created by 要江川 on 2023/7/23.
//

#include <iostream>
#include <vector>

using namespace std;

class Solution
{
public:
    int RemoveElementSelf(vector<int> &nums, int val)//普通暴力解法，速度较慢
    {
        int length = nums.size();
        for (int i = 0; i < length;) //比对i位置上的元素是否等于val
        {
            if (nums[i] == val) {
                for (int j = i; j < length - 1; ++j) {
                    nums[j] = nums[j + 1];
                }
                length--;
                continue;
            }
            ++i;
        }
        return length;
    }

    int RemoveElementDoublePointer(vector<int> &nums, int val)//双指针写法
    {
        int fast = 0, slow = 0;
        for (fast = 0; fast < nums.size(); ++fast) {
            if (nums[fast] != val) {
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }

    int RemoveElementDoublePointerImprove(vector<int>& nums, int val) //双指针优化
    {
        int left = 0, right = nums.size()-1;
        while (left <= right) {
            if (nums[left] == val) {
                nums[left] = nums[right];
                right--;
            } else {
                left++;
            }
        }
        return left;
    }

};

void Test()
{
    Solution s;
    int arr[3] = {2, 2, 3};
    vector<int> v(arr, arr + 3);
    int length = s.RemoveElementDoublePointerImprove(v, 3);
    cout << length << "\n";
    //vector<int>::iterator it=v.begin();
    auto it = v.begin();
    for (int i = 0; i < length; ++i, ++it) {
        cout << *it << " ";
    }
}

int main()
{
    Test();
}